<template>
    <view>
		<canvas :canvas-id="id" :id="id" :style="{width:width,height: height}" />
	</view>
</template>
<script>
	import QRCODE from '@/uni_modules/wmf-code/js_sdk';
	export default {
		name: 'WBarcode',
		props:{
			options:{
				type: Object,
				required: true,
				default: () =>{
					return {
						
					}
				}
			}
		},
		data () {
			return {
				id: 'WBarcode',
				height: null,
				width: null,
			}
		},
		created() {
			this.height = this.options.height + 'rpx';
			this.width = this.options.width + 'rpx';
			this.$nextTick(()=>{
				this.generateCode();
			})
		},
		watch: {
			options:{
				deep: true,
				handler (val) {
					this.height = val.height + 'rpx';
					this.width = val.width + 'rpx';
					// #ifdef H5
					setTimeout(()=>{// h5平台动态改变canvas大小
						this.generateCode();
					},50)
					// #endif
					
					// #ifdef APP-NVUE || APP-PLUS || MP || QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-HUAWEI || QUICKAPP-WEBVIEW-UNION
					this.generateCode();
					// #endif
				}
			}
		},
		methods: {
			generateCode () {
				try{
					const parameter = {...this.options,id: this.id,ctx: this};
					QRCODE.BarCode(parameter,(res)=>{
						this.$emit('generate',res)
					})
				}catch(err){}
			},
			async saveImg (){
				try{
					const res = await QRCODE.SaveImg({id: this.id,width: this.width,height: this.width,ctx: this});
					return res
				}catch(e){}
			}
		}
	}
</script>